<template>
   <div class="container">
    <p>空气质量</p>
    <p>空气质量指数：{{ data.indexes[0].aqiDisplay }}</p>
    <p>空气质量等级：{{ data.indexes[0].level }}</p>
    <p>空气质量类型：{{ data.indexes[0].category }}</p>
    <table>
        <tr>
            <th>污染物的名字</th>
            <th>污染物的全称</th>
            <th>污染物的浓度值</th>
            <th>浓度值单位</th>
        </tr>
        <tr v-for="(i, index) in data.pollutants" :key="index">
            <td>{{ i.name }}</td>
            <td>{{ i.fullName }}</td>
            <td>{{ i.concentration.value }}</td>
            <td>{{ i.concentration.unit }}</td>
        </tr>
    </table>
    <p>一般人群意见：{{ data.indexes[0].health.advice.generalPopulation }}</p>
    <p>敏感人群意见：{{ data.indexes[0].health.advice.sensitivePopulation }}</p>
   </div>
</template>

<script>
    import axios from 'axios';
    import { key } from '../common/constrant.js'

    export default{
        data(){
            return{
                data:[]
            }
        },

        mounted(){
            this.getData()
        },

        computed:{
            location:function(){
                return this.$store.state.location
            }
        },

        methods:{
            // 获取数据
            getData(){
                this.loc = Number(this.location.lon).toFixed(2) + "," + Number(this.location.lat).toFixed(2)
                // console.log(Number(this.location.lon).toFixed(2))
                axios.get("https://devapi.qweather.com/airquality/v1/current"+"/"+Number(this.location.lat).toFixed(2)+"/"+Number(this.location.lon).toFixed(2),{
                    params:{
                        key: key,
                    }   
                }).then((res) => {
                //   console.log(res.data)
                  this.data = res.data
                //   console.log(this.data.indexes[0].aqiDisplay)
                })
                
            },
            
        },

        watch:{
            location:function(){
                this.getData()
            }
        }
    }
</script>

<style>
  body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    margin: 0;
    padding: 20px;
  }
 
  .container {
    max-width: 800px;
    margin: 0 auto;
    background-color: #fff;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
 
  p {
    font-size: 18px;
    line-height: 1.5;
    margin-bottom: 15px;
  }
 
  table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
  }
 
  th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
  }
 
  th {
    background-color: #f2f2f2;
    font-weight: bold;
  }
 
  tr:nth-child(even) {
    background-color: #f9f9f9;
  }
 
  @media (max-width: 600px) {
    .container {
      padding: 10px;
    }
 
    p, table {
      font-size: 16px;
    }
  }
</style>